package com.G_CN06.mvc.controller.F10.F102;

import com.G_CN06.commonUtil.commonResultConfig.CommonResult;
import com.G_CN06.mvc.entity.*;
import com.G_CN06.mvc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author wcj
 * @className F1021
 * @description TODO
 * @since 2023/2/11 13:58
 **/

@RestController
@CrossOrigin
@RequestMapping("/testpaper")
public class F1021ManualController {

    @Autowired
    private TestPaperService testPaperService;

    @Autowired
    private ChoiceQuestionService choiceQuestionService;

    @Autowired
    private FillQuestionService fillQuestionService;

    @Autowired
    private JudgeQuestionService judgeQuestionService;

    @Autowired
    private MultiChoiceQuestionService multiChoiceQuestionService;

    @Autowired
    private SubjectivityQuestionService subjectivityQuestionService;

    @PostMapping("/create")
    public CommonResult createTestPaper(
            @RequestParam("testPaperName") String testPaperName,
            @RequestParam("testTime") int testTime,
            @RequestParam("totalScore") int totalScore,
            @RequestParam("topicId") int topicId,
            @RequestParam("classId") int classId,
            @RequestParam("numOfChoiceQuestions") int numOfChoiceQuestions,
            @RequestParam("numOfFillQuestions") int numOfFillQuestions,
            @RequestParam("numOfJudgeQuestions") int numOfJudgeQuestions,
            @RequestParam("numOfMultiChoiceQuestions") int numOfMultiChoiceQuestions,
            @RequestParam("numOfSubjectivityQuestions") int numOfSubjectivityQuestions,
            HttpServletRequest request) {

        // Get a list of questions for each question type based on the specified number of questions
        List<ChoiceQuestion> choiceQuestions = choiceQuestionService.getRandomQuestions(numOfChoiceQuestions);
        List<FillQuestion> fillQuestions = fillQuestionService.getRandomQuestions(numOfFillQuestions);
        List<JudgeQuestion> judgeQuestions = judgeQuestionService.getRandomQuestions(numOfJudgeQuestions);
        List<MultiChoiceQuestion> multiChoiceQuestions = multiChoiceQuestionService.getRandomQuestions(numOfMultiChoiceQuestions);
        List<SubjectivityQuestion> subjectivityQuestions = subjectivityQuestionService.getRandomQuestions(numOfSubjectivityQuestions);

        // Create a new test paper object and set its properties
        TestPaper testPaper = new TestPaper();
        testPaper.setName(testPaperName);
        testPaper.setTestTime(testTime);
        testPaper.setTotalScore(totalScore);

        // Associate the test paper with the specified topic and class
        testPaper.setTopicId(topicId);
        testPaper.setClassId(classId);

        // Add the selected questions to the test paper
        testPaper.setChoiceQuestions(choiceQuestions);
        testPaper.setFillQuestions(fillQuestions);
        testPaper.setJudgeQuestions(judgeQuestions);
        testPaper.setMultiChoiceQuestions(multiChoiceQuestions);
        testPaper.setSubjectivityQuestions(subjectivityQuestions);

        // Save the test paper to the database
        CommonResult result = testPaperService.saveForTeacher(testPaper, request.getSession());
        return result;
    }
}
